﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HXH.Redis;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace webapp.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        private readonly IRedisClient _redisClient;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IRedisClient redisClient)
        {
            _logger = logger;
            _redisClient = redisClient;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            _redisClient.Set("222", Encoding.UTF8.GetBytes("55555"), DateTime.Now.AddMinutes(7.0));
            var result = _redisClient.Get("222");
            Console.WriteLine(result);
            _redisClient.Set("key1", new WeatherForecast
            {
                Date = DateTime.Now.AddDays(1),
                TemperatureC = 55,
                Summary = Summaries[4]
            }, DateTime.Now.AddMinutes(10.0));

            var resultttt = _redisClient.Get<WeatherForecast>("key1");
            _redisClient.Remove("key1");
            Console.WriteLine(resultttt);
            _logger.LogDebug("这是一个调试");
            _logger.LogInformation("进入Get方法里面");
            _logger.LogWarning("这是一个警告");
            _logger.LogError("这是一条错误日志");
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}
